/**
 * Created by 游永年 on 2019-01-26 23:18.
 * 信息收集表单
 */


'use strict'
import React, {Component} from 'react';
import {connect} from 'dva';
import {createForm, createFormField} from 'rc-form';
import TagItems from "../../components/items/TagItems/TagItems";
import {Button, InputItem, List} from "antd-mobile";
import RadioGroup from "../../components/items/RadioGroup/RadioGroup";

// const isIPhone = new RegExp('\\biPhone\\b|\\biPod\\b', 'i').test(window.navigator.userAgent);
// let moneyKeyboardWrapProps;
// if (isIPhone) {
//   moneyKeyboardWrapProps = {
//     onTouchStart: e => e.preventDefault(),
//   };
// }

const opts = ({book, user}) => {
  const {filterData} = book || {};
  const {info} = book || {};
  return {info, filterData};
};

class Information extends Component {
  state = {
    type: 'money',
  }
  onChange = (value) => {
    console.log(value);
    this.setState({
      value,
    });
  }

  render() {
    const {getFieldProps} = this.props.form;
    const {
      filterData: {
        sbkc = [],
        bkfs = [{name: '上门辅导', value: 'smfd'}, {name: '门店辅导', value: 'mdfd'}],
      } = {},
    } = this.props;
    return (
      <div>
        <List>
          <InputItem
            {...getFieldProps('telphone')}
            placeholder="请输入"
          >联系方式</InputItem>

          <InputItem
            {...getFieldProps('name')}
            placeholder="请输入"
          >学生名称</InputItem>

          <TagItems {...getFieldProps('bklr')} data={sbkc} type="multi">补课内容</TagItems>

          <RadioGroup {...getFieldProps('bkfs')} data={bkfs} type="multi">补课方式</RadioGroup>

          <InputItem
            {...getFieldProps('smdz')}
            placeholder="上门地址"
          >学生名称</InputItem>
          <InputItem
            {...getFieldProps('bz')}
            placeholder="请输入"
            type={'textarea'}
          >其它备注</InputItem>
        </List>
        <Button onClick={this.onOk} type="primary">确定</Button>
      </div>
    );
  }

  reset = _ => this.props.dispatch({type: 'book/info', payload: {}});

  onOk = _ => this.props.dispatch({type: 'book/filterVisible'});
}

Information.propTypes = {};

const onFieldsChange = (props, fields) => {
  const {dispatch} = props;
  let {info = {}} = props;
  info = {...info, ...fields};
  dispatch({type: 'user/info', payload: info});
};

export const mapPropsToFields = (props) => {  //  用于更新页面数据，否则重新赋值刷新不了页面
  const {info = {}} = props;
  const obj = {};
  const fields = ['telphone', 'name', 'bklr', 'bkfs', 'smdz', 'bz'];
  fields.forEach(field => {
    const fieldValue = Reflect.get(info, field);
    Reflect.set(obj, field, createFormField(fieldValue));
  });
  return obj;
};

export default connect(opts)(createForm({mapPropsToFields, onFieldsChange})(Information));
